﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace sqf
{
    internal class Statistic
	{
		///
		/// \brief 曲线拟合类
		///

		public double std_dev;                 // 样本的标准差
		public double std_error;               //
		public double mean;// 样本均值
		public double sum; // 样本总和
		public double cv; // coefficient of variation 变异系数

		public Statistic()
        {
			std_dev =0; std_error =0; mean = 0; sum = 0; cv = 0; // 构造函数，初始化
		}

		public double calcStdDev(List<double> x)
			{
			double sumX = 0, avgX = 0, Xtmp = 0;
				for (int n = 0; n < x.Count; n++)
				{
					sumX += x[n];
				}
				avgX = sumX / x.Count;
				for (int n = 0; n < x.Count; n++)
				{
					Xtmp += (x[n] - avgX) * (x[n] - avgX);
				}
				std_dev = Math.Sqrt(Xtmp / (x.Count - 1));

				return std_dev;
			}

			
			double calcStError(List<double> x)
			{
				double sumX = 0, avgX = 0, Xtmp = 0;
				for (int n = 0; n < x.Count; n++)
				{
					sumX += x[n];
				}
				avgX = sumX / x.Count;
				for (int n = 0; n < x.Count; n++)
				{
					Xtmp += (x[n] - avgX) * (x[n] - avgX);
				}
				std_dev = Math.Sqrt(Xtmp / (x.Count - 1));
				std_error = std_dev / Math.Sqrt(x.Count);
				return std_error;
			}

			public double calcCV(List<double> x) // coefficient of variation (C. V.)
			{
				double sumX = 0, avgX = 0, Xtmp = 0;
				for (int n = 0; n < x.Count; n++)
				{
					sumX += x[n];
				}
				avgX = sumX / x.Count;
				for (int n = 0; n < x.Count; n++)
				{
					Xtmp += (x[n] - avgX) * (x[n] - avgX);
				}
				std_dev = Math.Sqrt(Xtmp / (x.Count - 1));
				if (avgX == 0)
				{
					cv = 0; // if avgX is 0, cv can NOT be calculated, so we give a 0 return. 
				}
				else
				{
					cv = std_dev / avgX * 100; // calculation of coefficient of variation (c.v.) SD/mean * 100%.
				}

				return cv;
			}

			public double calcMean(List<double> x)
			{
				double sumX = 0, avgX = 0;
				for (int n = 0; n < x.Count; n++)
				{
					sumX += x[n];
				}
				avgX = sumX / x.Count;
				return avgX;
			}
		};
}
